



//
//  MBFirstShopVC.m
//  房源详情-小区详情
//
//  Created by MB__Lee on 2018/5/30.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "MBFirstShopVC.h"

#import "MBNetTool.h"
#import "BaseListTableview.h"

#import <MJRefresh.h>
#import <MJExtension.h>



#import "SearchViewFactory.h" //头部搜索条件过滤

#import "MBBarSearchView.h" //导航栏 搜索框
@interface MBFirstShopVC ()<MBBarSearchViewDelegate,BaseSearchViewDelegate>

@property(nonatomic,strong)BaseListTableview *baseTableView;

@property(nonatomic,strong)MBBarSearchView *BarSearchView;// 导航栏上的搜索框

@property(nonatomic,strong)BaseHousePostModel *postModel;// 请求参数模型
@property(nonatomic,assign)NSUInteger page; //请求页数

@end

@implementation MBFirstShopVC


-(BaseListTableview *)baseTableView{
    if (!_baseTableView) {
        
        CGFloat tableview_y = CGRectGetMaxY(self.navigationController.navigationBar.frame);
        tableview_y += 49;
        
        CGRect baseFrame =  CGRectMake(0, tableview_y, SCREEN_WIDTH, SCREEN_HIGHT - tableview_y);
        _baseTableView = [[BaseListTableview alloc] initWithFrame:baseFrame style:UITableViewStylePlain];
        
        
        __weak typeof(self) weakSelf = self;
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf.baseTableView.BaseHouseMut removeAllObjects];
            [self getHouseSourceFromNetWithPage:1 postModel:_postModel];
        }];
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            _page += 1;
            [self getHouseSourceFromNetWithPage:_page postModel:_postModel];
        }];
        
        _baseTableView.mj_header = header;
        _baseTableView.mj_footer = footer;
        [_baseTableView.mj_header beginRefreshing];
        
        
    }
    return  _baseTableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"商铺";
    
    _postModel = [[BaseHousePostModel alloc] init];
    _postModel.category = @"1004";
    _postModel.city = MBCityCode;
    
    [self addSearchCondition]; //添加搜索过滤条件
    [self addNavibationBarSearchText];//添加导航栏搜索空间
    [self setNavigationBar_House];// 设置导航栏
    
}
#pragma mark ---添加搜索过滤条件
-(void)addSearchCondition{
    CGFloat searchView_Y = CGRectGetMaxY(self.navigationController.navigationBar.frame);
    
    
    BaseSearchView *searchView =  [SearchViewFactory creatSearViewWithFrame:CGRectMake(0, searchView_Y, SCREEN_WIDTH, 49) searViewType:SearViewTypeShop];
    searchView.delegate = self;
    [self.view addSubview:searchView];
    [self.view insertSubview:self.baseTableView belowSubview:searchView];
}

#pragma mark --添加导航栏搜索头
-(void)addNavibationBarSearchText{
    
    MBBarSearchView *searchView = [MBBarSearchView MBBarSearchViewCreat];
    _BarSearchView  = searchView;
    searchView.mbBarDelegate = self;
    searchView.frame = self.navigationController.navigationBar.bounds;
    [self.navigationController.navigationBar addSubview:searchView];
    
    
    
}
#pragma mark -- 设置导航栏
-(void)setNavigationBar_House{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"back_icon"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backBTN_house) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *BackBar = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = BackBar;
}
-(void)backBTN_house{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -- 从网络获取数据
-(void)getHouseSourceFromNetWithPage:(NSUInteger)page postModel:(BaseHousePostModel *)model{
    
    _page = page;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"category"] = model.category;
    dict[@"city"] = model.city;
    dict[@"region"] = model.region;
    dict[@"square_start"] = model.square_start;
    dict[@"square_end"] = model.square_end;
    dict[@"bedroom"] = model.bedroom;
    dict[@"type"] = model.type;
    dict[@"characteristic"] = model.characteristic;
    dict[@"decoration"] = model.decoration;
    dict[@"orientation"] = model.orientation;
    dict[@"title"] = model.title;
    dict[@"name"] = model.name;
    dict[@"xq"] = model.xq;
    dict[@"member"] = model.member;
    dict[@"date_start"] = model.date_start;
    dict[@"date_end"] = model.date_end;
    dict[@"order"] = model.order;
    dict[@"page"] = [NSString stringWithFormat:@"%zd",page];
    dict[@"search"] = model.search;
    
    
    [MBNetTool MBPost:Fang_list paramater:dict success:^(id response) {
        
        if ([response[@"code"] integerValue] == 10000) {
            NSMutableArray *mut = [BaseHouseModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
            
            
            [self.baseTableView addSource:mut]; //此方法内部进行刷新
            [self.baseTableView.mj_header endRefreshing];
            [self.baseTableView.mj_footer endRefreshing];
            
            
            
        }else if([response[@"code"] integerValue] == 50000){
            
            [self.baseTableView.mj_header endRefreshing];
            [self.baseTableView.mj_footer endRefreshing];
            
            
            NSLog(@"没有更多数据");
            
            
        }else{
            
            [self.baseTableView.BaseHouseMut removeAllObjects];
            [self.baseTableView reloadData];
            [self.baseTableView.mj_header endRefreshing];
            [self.baseTableView.mj_footer endRefreshing];
        }
        
    } failure:^(NSError *error) {
        [self.baseTableView.mj_header endRefreshing];
        [self.baseTableView.mj_footer endRefreshing];
    }];
    
    
    
}



#pragma mark --BaseSearchViewDelegate --筛选框
-(void)SearchView:(BaseSearchView *)searchView sendPostModel:(BaseHousePostModel *)model{
    model.category = @"1004";
    model.city = MBCityCode;
    _postModel  = model;
    _page = 1;
    
    [self.baseTableView.BaseHouseMut removeAllObjects];
    [self.baseTableView reloadData];
    
    [self getHouseSourceFromNetWithPage:1 postModel:_postModel];
    
}

#pragma mark --MBBarSearchViewDelegate --导航栏搜索框
-(void)MBBarSearchViewSendSearchText:(NSString *)text{
    _postModel.search = text;
    _page = 1;
    
    NSLog(@"%@",text);
    [self.baseTableView.BaseHouseMut removeAllObjects];
    [self.baseTableView reloadData];
    [self getHouseSourceFromNetWithPage:1 postModel:_postModel];
}

- (void)MBBarSearchViewBack {
    [self backBTN_house];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [_BarSearchView removeFromSuperview];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
